<template>
	<view class="index-container">
		<view class="business">
			<image src="../static/banner.png" mode=""></image>
			<view class="task text"><text style="margin-right: 30rpx;">巡检统计</text><text style="color: #F2FF63;"
					@click="refresh">刷新</text></view>
			<!-- <navigator url="../checkPointEdit/checkPointEdit">新建检查点</navigator> -->
			<view class="task-num">
				<view class="num-item">
					<view class="num-left">{{allList.length}}</view>
					<view>总任务</view>
				</view>
				<view class="num-item">
					<view class="num-center">{{complete}}</view>
					<view>已完成</view>
				</view>
				<view class="num-item">
					<view class="num-right">{{handle}}</view>
					<view>待完成</view>
				</view>
			</view>
		</view>
		<!-- 导航 -->
		<view class="nav">
			<view class="nav-item" v-for="item in navList" :key="item.id" @click="navItemClick(item.path)">
				<view class="item-img">
					<image :src="item.icon" mode=""></image>
				</view>
				<text>{{item.text}}</text>
			</view>
		</view>
		<view class="check-task">
			<text style="font-size: 36rpx;color: #333;">巡检任务</text>
			<view class="more" @click="goCheckTask">查看更多</view>
		</view>
		<!-- 巡检任务 -->
		<check-item @detail="goCheck" v-if="checkList" :check-list="checkList"></check-item>
		<view v-else>
			<van-empty description="暂无任务" />
		</view>
		<uni-load-more v-if="checkList && checkList.length >= 5" :status="status" />

	</view>
</template>

<script>
	import CheckItem from '../components/checkItem.vue'
	import {
		mapState
	} from 'vuex'
	import dayjs from 'dayjs'
	export default {
		components: {
			CheckItem
		},
		data() {
			return {
				allList: [],
				status: 'more',
				status1: 'more',
				title: 'Hello',
				checkList: null,
				rectifyList: null,
				params: {
					巡检类型: null,
					所属网格id: null,
					网格员id: null,
					巡检状态: 1,
					pagination: {
						current: 1,
						pageSize: 5
					},
					sorter: {
						field: "巡检优先级",
						order: 1
					}
				},
				params1: {
					巡检类型: null,
					所属网格id: null,
					网格员id: null,
					巡检状态: 0,
				},
				params2: {
					所属网格id: null,
					整改状态: 2,
					负责人电话: null,
					pagination: {
						current: 1,
						pageSize: 2
					},
					sorter: {
						field: "id",
						order: 0
					}
				},
				navList: [{
					id: 1,
					icon: '../static/1.png',
					text: '信息采集',
					path: '/fireGridCheck/collectMessage/collectMessage'
				}, {
					id: 2,
					icon: '../static/3.png',
					text: '整改任务',
					path: '/fireGridCheck/rectifyTask/rectifyTask'
				}, {
					id: 3,
					icon: '../static/4.png',
					text: '新建整改',
					path: '/fireGridCheck/createRectify/createRectify'
				}, {
					id: 4,
					icon: '../static/5.png',
					text: '新建巡检',
					path: '/fireGridCheck/createCheck/createCheck'
				},{
					id: 5,
					icon: '../static/2.png',
					text: '记录列表',
					path: '/fireGridCheck/allRecordList/allRecordList'
				}]
			}
		},
		onLoad(options) {
			if (options.q) {
				console.log(options.q)
				let str = decodeURIComponent(options.q)
				const index = str.indexOf('{')
				let newStr = str.slice(index)
				// let obj = JSON.parse(newStr)
				uni.setStorageSync('params', newStr)
				// console.log(obj.type, obj.id)
				// console.log(typeof obj)
				// console.log(str)
			}
			if (uni.getStorageSync('login') && uni.getStorageSync('params')) {
				let params = JSON.parse(uni.getStorageSync('params'))
				this.$common.scanFun(params)
			}
			if (!uni.getStorageSync('login')) {
				this.showModal()
			}
		},
		onShow() {
			// this.$common.setTitle(this.$config.communityName)
			if (uni.getStorageSync('login')) {

				this.init()
			}
		},
		onReachBottom() {
			console.log('触发上拉')
			if (this.status !== 'noMore' && this.checkList) {
				this.status = 'loading'
				this.getMoreCheckTask()
			}
		},
		computed: {
			...mapState(['personType']),
			complete() {
				return this.allList.filter(item => item.巡检状态 == 3).length
			},
			handle() {
				return this.allList.filter(item => [1, 2].includes(item.巡检状态)).length
			}
		},
		onTabItemTap(e) {
			console.log(e)
		},
		methods: {
			showModal() {
				uni.showModal({
					title: '提示',
					content: '您还没有登录,请登录后使用',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/fireGridCheck/identityChoose/identityChoose'
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			navItemClick(url) {
				if (!uni.getStorageSync('login')) {
					this.showModal()
				} else {
					uni.navigateTo({
						url
					})
				}
			},
			refresh() {
				this.init()
			},
			goMoreRectify() {
				console.log('点击触发')
				uni.navigateTo({
					url: '/fireGridCheck/moreRectifyTask/moreRectifyTask'
				})
			},
			goCheckRank() {
				uni.navigateTo({
					url: '/fireGridCheck/checkRank/checkRank'
				})
			},
			goCheckItem() {
				uni.navigateTo({
					url: '/fireGridCheck/inspectItem/inspectItem'
				})
			},
			goCheck(item) {
				uni.navigateTo({
					url: '/fireGridCheck/checkList/checkList?item=' + JSON.stringify(item)
				})
			},
			async init() {
				this.params.所属网格id = uni.getStorageSync('gridInfo').所属网格id
				this.params1.所属网格id = uni.getStorageSync('gridInfo').所属网格id
				// this.params.网格员id = userInfo.id
				this.checkList = await this.getCheckTask()
				this.allList = await this.getAllCheck() || []
				// this.getAllCheck()
			},
			getAllCheck() {
				return new Promise((resolve, reject) => {
					this.$http.post('gridSystem/inspection/list', this.params1).then(res => {
						console.log(res)
						if (res.code === 10000) {
							resolve(res.data.list)
						}
					})
				})
			},
			getCheckTask() {
				this.params.pagination.current = 1
				this.status = 'more'
				return new Promise((resolve, reject) => {
					uni.showLoading({
						mask: true
					})
					this.$http.post('gridSystem/inspection/list', this.params).then(res => {
						if (res.code === 10000) {
							console.log(res)
							resolve(res.data.list)
						}
					})
				})
			},
			getMoreCheckTask() {
				this.params.pagination.current++
				this.$http.post('gridSystem/inspection/list', this.params).then(res => {
					if (res.code === 10000) {
						if (res.data.list !== null) {
							this.checkList = this.checkList.concat(res.data.list)
						}
						// this.checkList = this.checkList.concat(res.data.list)
						if (this.checkList.length === res.data.total) {
							this.status = 'noMore'
						}
					}
				})
			},
			goCheckTask() {
				uni.navigateTo({
					url: '/fireGridCheck/checkTask/checkTask'
				})
			},
			getUserInfo() {
				return new Promise((resolve, reject) => {
					this.$http.post('gridSystem/gridAdmin/info', {
						id: 0
					}).then(res => {
						if (res.code === 10000) {
							resolve(res.data)
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../style/index.scss';
</style>
